package cn.com.fuhuitong.main.home.ui.activity

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import cn.com.fuhuitong.R
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.home.vm.EnrollViewModel
import kotlinx.android.synthetic.main.activity_enroll_welfare.*

class EnrollWelfareActivity : ViewModeActivity<EnrollViewModel>() {

    override fun viewModel(): EnrollViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_enroll_welfare

    override fun initView() {
        image_enroll_back.setOnClickListener {
            finish()
        }

        text_enroll_submit.setOnClickListener {
            enrollWelfare()
        }

    }

    override fun initViewMode() {
        viewModel.baseDataLiveData.observe(this as LifecycleOwner, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            toast(response.response?.msg ?: "报名成功")

            finish()
        })
    }

    private fun enrollWelfare() {

        val name = edit_enroll_name.text.toString()
        var sex: Int = 1
        if (radio_enroll_sex_nan.isChecked)
            sex = 1
        if (radio_enroll_sex_nv.isChecked)
            sex = 0
        val year = edit_enroll_year.text.toString()
        val address = edit_enroll_address.text.toString()
        val id_num = edit_enroll_id_num.text.toString()
        val mobile = edit_enroll_mobile.text.toString()
        val objective = edit_enroll_objective.text.toString()

        if (
            name.isNullOrEmpty() ||
            year.isNullOrEmpty() ||
            address.isNullOrEmpty() ||
            id_num.isNullOrEmpty() ||
            mobile.isNullOrEmpty() ||
            objective.isNullOrEmpty()
        ) {
            toast("请输入完整信息")
        } else {
            viewModel.enrollWelfare(
                name = name,
                sex = sex.toString(),
                year = year,
                address = address,
                id_num = id_num,
                mobile = mobile,
                objective = objective
            )
        }
    }

}